package cn.dd.crmweb.modules.sys.controller;

import cn.dd.crmweb.common.Result;
import cn.dd.crmweb.common.constans.Constans;
import cn.dd.crmweb.modules.sys.dto.UserDto;
import cn.dd.crmweb.modules.sys.entity.Role;
import cn.dd.crmweb.modules.sys.entity.User;
import cn.dd.crmweb.modules.sys.mapper.RoleMenuMapper;
import cn.dd.crmweb.modules.sys.service.IRoleService;
import cn.dd.crmweb.modules.sys.service.IUserService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController

@RequestMapping("/sys/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    //查询所有
    @GetMapping("/list")
    public List<Role> findAll(){
        return  roleService.list();
    }

//    @GetMapping("/fetch")
//    public Result findOne(@RequestBody String roleCode ){
////        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
////        queryWrapper.eq("rolecode",role.getRolecode());
//        return Result.success(roleService.getRole(roleCode));
//    }

    //新增或修改
    @PostMapping("/add")
    public boolean save(@RequestBody Role role){
        return  roleService.saveOrUpdate(role);
    }

    @DeleteMapping("{id}/delete")
    public boolean delete(@PathVariable("id") String id ) {
        roleMenuMapper.deleteByRoleId(id);
        return  roleService.removeById(id);
    }

    @DeleteMapping ("batch/delete")
    public boolean deleteBatch(@RequestBody List<String> ids){
        return  roleService.removeByIds(ids);
    }

    /**
     * 绑定角色和菜单的关系
     * @param roleId  角色id
     * @param menuIds  菜单id数组
     * @return
     */
    @PostMapping("/roleMenu/{roleId}")
    public Result roleMenu(@PathVariable String  roleId,@RequestBody List<String> menuIds){
        roleService.setRoleMenu(roleId,menuIds);
        return  Result.success();
    }

    /**
     *   查找角色拥有的菜单
     * @param roleId
     * @return
     */
    @GetMapping("/getMenu/{roleId}")
    public Result getRoleMenu(@PathVariable("roleId") String  roleId) {
        return  Result.success(roleService.getRoleMenu(roleId));
    }

}
